package main

import (
	"bytes"
	"fmt"
	"log"
	"os"

	"github.com/gin-contrib/cors"
	"github.com/gin-gonic/gin"

	"github.com/joho/godotenv"
	"tools.hezhuyun/checker/infrastructure/persistence"
	"tools.hezhuyun/checker/interfaces"
)

func init() {
	var err error
	env := os.Getenv("GOENV")

	var (
		buf    bytes.Buffer
		logger = log.New(&buf, "logger: ", log.Lshortfile)
	)
	logger.Printf("hezhuyun-tools start up with env: %s", env)

	switch env {
	case "uat":
		err = godotenv.Load(".env.uat")
	case "local":
		err = godotenv.Load(".env.local")
	default:
		gin.SetMode(gin.ReleaseMode)
	}

	if err != nil {
		panic(err)
	}

	err = godotenv.Load()
	if err != nil {
		log.Fatal("Error Loading .env file")
	}
}

func main() {
	router := gin.New()
	router.MaxMultipartMemory = 200 << 20 //200MiB
	//router := gin.Default()
	router.Use(cors.Default())
	router.Use(gin.Logger())

	services, err := persistence.NewRepositories()
	if err != nil {
		log.Fatal(err)
	}
	checker := interfaces.NewChecker(services.File, services.Folder)
	router.GET("/fileinfo", checker.GetFile)
	router.GET("/folder", checker.GetFolder)
	router.GET("/check/:folder_id", checker.CheckFolder)

	s3Bucket := os.Getenv("S3_BUCKET")
	fmt.Printf("start main %s", s3Bucket)

	router.Run(":8081")
}
